package dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import dao.OrderDao;
import dbpool.DbPool;
import entity.OrderDetails;

public class OrderDaoImpl implements OrderDao{
	DbPool pool = DbPool.getPool();
	/**
	 * 生成订单表
	 */
	@Override
	public int insertOrder(int user_id, int goods_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "insert into order values(null,?,?)";
		QueryRunner runner = new QueryRunner();
		int res = runner.update(conn, sql, goods_id,user_id);
		pool.back(conn);
		return res;
	}
	/*
	 * 一个用户所有的订单id
	 */
	@Override
	public List<Integer> getOrderIdByUser(int user_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "select order_id from order where user_id = ?";
		QueryRunner runner = new QueryRunner();
		List<Integer> idList = (List<Integer>) runner.query(conn, sql, new BeanListHandler<Integer>(Integer.class),user_id);
		pool.back(conn);
		return idList;
	}
	
	/**
	 * 找到对应订单id的商品id
	 */
	@Override
	public List<Integer> getGoodIdByOrderId(int order_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "select goods_id from order where order_id = ?";
		QueryRunner runner = new QueryRunner();
		List<Integer> idList = (List<Integer>) runner.query(conn, sql, new BeanListHandler<Integer>(Integer.class),order_id);
		pool.back(conn);
		return idList;
	}
	/**
	 * 查询订单详情
	 */
	@Override
	public OrderDetails selOrderDetails(int order_id) throws SQLException {
		Connection conn = pool.getConn();
		System.out.println(conn);
		String sql = "select * from orderdetails where order_id = ? ";
		QueryRunner runner = new QueryRunner();
		OrderDetails od = (OrderDetails) runner.query(conn, sql, new BeanHandler<OrderDetails>(OrderDetails.class),
				order_id);
		pool.back(conn);
		return od;
	}
	/**
	 * 删除普通订单
	 */
	@Override
	public int delOrderByOrder_id(int order_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "delete from order where order_id = ?";
		QueryRunner runner = new QueryRunner();
		int res = runner.update(conn, sql, order_id);
		pool.back(conn);
		return res;
	}
	/**
	 * 生成详细订单
	 */
	@Override
	public int insertIntoOrderDel(int user_id, int order_id, int total, int num, int state, String sTime, String eTime,
			int gift) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "insert into order values(null,?,?,?,?,?,?,?)";
		QueryRunner runner = new QueryRunner();
		int res = runner.update(conn, sql, order_id,user_id,total,num,state,sTime,eTime,gift);
		pool.back(conn);
		return res;
	}
	/**
	 * 删除详细订单
	 */
	@Override
	public int delOrderDelByOrder_id(int order_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "delete from orderdetails where order_id = ?";
		QueryRunner runner = new QueryRunner();
		int res = runner.update(conn, sql, order_id);
		pool.back(conn);
		return res;
	}
	/**
	 * 修改某订单状态
	 */
	@Override
	public int updateStatus(int status, int order_id) throws SQLException {
		Connection conn = pool.getConn();
		String sql = "update orderdetails set state = ? where order_id = ?";
		QueryRunner runner = new QueryRunner();
		int res = runner.update(conn, sql,status, order_id);
		pool.back(conn);
		return res;
	}
	@Override
	public List<OrderDetails> getAllOrderByid(int user_id) throws SQLException {
		Connection conn = pool.getConn();
		System.out.println(conn);
		String sql = "select * from orderdetails where user_id = ? ";
		QueryRunner runner = new QueryRunner();
		List<OrderDetails> odList = (List<OrderDetails>) runner.query(conn, sql, new BeanListHandler<OrderDetails>(OrderDetails.class),
				user_id);
		pool.back(conn);
		return odList;
	}

}
